<template>
  <div class="navbar navbar-default">
    <div class="container-fluid">
      <!-- Brand and toggle get grouped for better mobile display -->
      <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
          <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="#">全部商品分类</a>
      </div>

      <!-- Collect the nav links, forms, and other content for toggling -->
      <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav">
          <li class="active"><a href="#">首页<span class="sr-only">(current)</span></a></li>
          <li><a href="#">管理学</a></li>
          <li><a href="#">理学类</a></li>
          <li><a href="#">工学类</a></li>
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
               aria-expanded="false">Dropdown <span class="caret"></span></a>
            <ul class="dropdown-menu">
              <li><a href="#">Action</a></li>
              <li><a href="#">Another action</a></li>
              <li><a href="#">Something else here</a></li>
              <li role="separator" class="divider"></li>
              <li><a href="#">Separated link</a></li>
              <li role="separator" class="divider"></li>
              <li><a href="#">One more separated link</a></li>
            </ul>
          </li>
        </ul>
        <form class="navbar-form navbar-left">
          <div class="form-group">
            <input type="text" class="form-control" placeholder="Search">
          </div>
          <button type="submit" class="btn btn-default">Submit</button>
        </form>
        <ul class="nav navbar-nav navbar-right">

          <li v-if="!username"><a @click="login">登录</a></li>
          <li v-if="!username"><a @click="register">注册</a></li>


          <li class="dropdown" v-else>
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
               aria-expanded="false">{{ username }} <span class="caret"></span></a>
            <ul class="dropdown-menu">
              <li><a @click="logout">注销</a></li>
              <li><a href="#">Another action</a></li>
              <li><a href="#">Something else here</a></li>
              <li role="separator" class="divider"></li>
              <li><a href="#">Separated link</a></li>
            </ul>
          </li>


        </ul>
      </div><!-- /.navbar-collapse -->
<!--  登录组件-->
      <Login @close="close_login" @go="put_register" @loginsuccess="login_success" v-if="is_login"/>
      <Register  @close="close_register" @go="put_login" v-if="is_register"/>
    </div><!-- /.container-fluid -->
  </div>
</template>

<script>
import Login from "@/components/Login";
import Register from "@/components/Register";
export default {
  name: "Head",
  data() {
    sessionStorage.aaa = 'ok';
    return {
      url_path: sessionStorage.url_path || '/',
      is_login: false,
      is_register: false,
      token: '',
      username: '',

    }
  },
  methods: {
    goPage(url_path) {
      // 传入的路由如果不是当前所在路径，就跳转
      if (this.url_path !== url_path) {
        this.$route.push(url_path);
      }
      sessionStorage.url_path = url_path;
    },
    login(){
      this.is_login = true;
    },
    close_login(){
      this.is_login = false;
    },
    register(){
      this.is_register = true;
    },
    close_register(){
      this.is_register = false;
    },
    put_register(){
      console.log('ok')
      this.is_login = false;
      this.is_register = true;
    },
    put_login(){
      console.log('ok')
      this.is_login = true;
      this.is_register = false;
    },
    login_success(){
      this.username = this.$cookies.get('username')
      this.token = this.$cookies.get('token')
    },
    logout(){
      // 清除cookies
      this.$cookies.remove('username')
      this.$cookies.remove('token')
      // 把username 和 token 变量置空
      this.username = ''
      this.token = ''

    },
  },
  created() {
    sessionStorage.url_path = this.$route.path;
    this.url_path = this.$route.path;

    //当页面一创建就去cookies中取token和username
    this.username = this.$cookies.get('username')
    this.token = this.$cookies.get('token')

  },
  components: {
    Login,
    Register
}
}
</script>

<style scoped>

</style>